'\" t
.\"     Title: CREATE VIEW
.\"    Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
.\"      Date: 2011-12-01
.\"    Manual: PostgreSQL 9.1.2 Documentation
.\"    Source: PostgreSQL 9.1.2
.\"  Language: English
.\"
.TH "CREATE VIEW" "7" "2011-12-01" "PostgreSQL 9.1.2" "PostgreSQL 9.1.2 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
CREATE_VIEW \- define a new view
.\" CREATE VIEW
.SH "SYNOPSIS"
.sp
.nf
CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW \fIname\fR [ ( \fIcolumn_name\fR [, \&.\&.\&.] ) ]
    AS \fIquery\fR
.fi
.SH "DESCRIPTION"
.PP

CREATE VIEW
defines a view of a query\&. The view is not physically materialized\&. Instead, the query is run every time the view is referenced in a query\&.
.PP

CREATE OR REPLACE VIEW
is similar, but if a view of the same name already exists, it is replaced\&. The new query must generate the same columns that were generated by the existing view query (that is, the same column names in the same order and with the same data types), but it may add additional columns to the end of the list\&. The calculations giving rise to the output columns may be completely different\&.
.PP
If a schema name is given (for example,
CREATE VIEW myschema\&.myview \&.\&.\&.) then the view is created in the specified schema\&. Otherwise it is created in the current schema\&. Temporary views exist in a special schema, so a schema name cannot be given when creating a temporary view\&. The name of the view must be distinct from the name of any other view, table, sequence, index or foreign table in the same schema\&.
.SH "PARAMETERS"
.PP
TEMPORARY or TEMP
.RS 4
If specified, the view is created as a temporary view\&. Temporary views are automatically dropped at the end of the current session\&. Existing permanent relations with the same name are not visible to the current session while the temporary view exists, unless they are referenced with schema\-qualified names\&.
.sp
If any of the tables referenced by the view are temporary, the view is created as a temporary view (whether
TEMPORARY
is specified or not)\&.
.RE
.PP
\fIname\fR
.RS 4
The name (optionally schema\-qualified) of a view to be created\&.
.RE
.PP
\fIcolumn_name\fR
.RS 4
An optional list of names to be used for columns of the view\&. If not given, the column names are deduced from the query\&.
.RE
.PP
\fIquery\fR
.RS 4
A
\fBSELECT\fR(7)
or
\fBVALUES\fR(7)
command which will provide the columns and rows of the view\&.
.RE
.SH "NOTES"
.PP
Currently, views are read only: the system will not allow an insert, update, or delete on a view\&. You can get the effect of an updatable view by creating rules that rewrite inserts, etc\&. on the view into appropriate actions on other tables\&. For more information see
CREATE RULE (\fBCREATE_RULE\fR(7))\&.
.PP
Use the
DROP VIEW (\fBDROP_VIEW\fR(7))
statement to drop views\&.
.PP
Be careful that the names and types of the view\*(Aqs columns will be assigned the way you want\&. For example:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW vista AS SELECT \*(AqHello World\*(Aq;
.fi
.if n \{\
.RE
.\}
.sp
is bad form in two ways: the column name defaults to
?column?, and the column data type defaults to
unknown\&. If you want a string literal in a view\*(Aqs result, use something like:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW vista AS SELECT text \*(AqHello World\*(Aq AS hello;
.fi
.if n \{\
.RE
.\}
.PP
Access to tables referenced in the view is determined by permissions of the view owner\&. In some cases, this can be used to provide secure but restricted access to the underlying tables\&. However, not all views are secure against tampering; see
Section 37.4, \(lqRules and Privileges\(rq, in the documentation
for details\&. Functions called in the view are treated the same as if they had been called directly from the query using the view\&. Therefore the user of a view must have permissions to call all functions used by the view\&.
.PP
When
CREATE OR REPLACE VIEW
is used on an existing view, only the view\*(Aqs defining SELECT rule is changed\&. Other view properties, including ownership, permissions, and non\-SELECT rules, remain unchanged\&. You must own the view to replace it (this includes being a member of the owning role)\&.
.SH "EXAMPLES"
.PP
Create a view consisting of all comedy films:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW comedies AS
    SELECT *
    FROM films
    WHERE kind = \*(AqComedy\*(Aq;
.fi
.if n \{\
.RE
.\}
.sp
This will create a view containing the columns that are in the
film
table at the time of view creation\&. Though
*
was used to create the view, columns added later to the table will not be part of the view\&.
.SH "COMPATIBILITY"
.PP
The SQL standard specifies some additional capabilities for the
CREATE VIEW
statement:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE VIEW \fIname\fR [ ( \fIcolumn_name\fR [, \&.\&.\&.] ) ]
    AS \fIquery\fR
    [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
.fi
.if n \{\
.RE
.\}
.PP
The optional clauses for the full SQL command are:
.PP
CHECK OPTION
.RS 4
This option has to do with updatable views\&. All
INSERT
and
UPDATE
commands on the view will be checked to ensure data satisfy the view\-defining condition (that is, the new data would be visible through the view)\&. If they do not, the update will be rejected\&.
.RE
.PP
LOCAL
.RS 4
Check for integrity on this view\&.
.RE
.PP
CASCADED
.RS 4
Check for integrity on this view and on any dependent view\&.
CASCADED
is assumed if neither
CASCADED
nor
LOCAL
is specified\&.
.RE
.PP

CREATE OR REPLACE VIEW
is a
PostgreSQL
language extension\&. So is the concept of a temporary view\&.
.SH "SEE ALSO"
ALTER VIEW (\fBALTER_VIEW\fR(7)), DROP VIEW (\fBDROP_VIEW\fR(7))
